<!-- template 模块和视图区域 -->
<template>
    <div class="todo-app" id="app-test">
        <div class="title">App</div>

        <div class="todo-from">
            <input
                v-model="inputValue"
                class="todo-input"
                type="text"
                placeholder="Add a Todo"
            />
            <div @click="add" class="todo-button">Add</div>
        </div>

        <div
            v-bind:class="[item.isComplete ? 'complete' : 'item']"
            v-for="(item, index) in list"
            :key="index"
        >
            <div>
                <input
                    @click="updated(item._id)"
                    v-model="item.isComplete"
                    type="checkbox"
                />
                <span class="name">{{ item.value }}</span>
            </div>

            <div class="del" @click="del(item._id)">删除</div>
        </div>
        <myButton :btname="testName" @innerClick="handlerMsg"></myButton>
    </div>
</template>

<!-- script 脚本(模块和逻辑)区域 -->
<script>
import myButton from './button.vue'
import axios from 'axios'
export default {
    name: 'test',
    components: { myButton },
    data() {
        return {
            inputValue: '',
            list: [],
            testName: 'props向子组件传值测试'
        }
    },
    created() {
        console.log('test组件创建完毕')

        this.getList()
    },
    methods: {
        handlerMsg(str) {
            alert('点击了子组件==' + str)
        },
        async add() {
            //添加列表调用push
            // this.list.push({
            //     isComplete: false,
            //     text: this.value
            // })
            // this.value = ''
            await axios({
                url: 'https://ppdyh35v5v.hzh.sealos.run/add_todo',
                method: 'post',
                data: {
                    value: this.inputValue, // 获取输入的值
                    isComplete: false // 是否选中
                }
            })
            // 重新获取列表
            this.getList()
            this.inputValue = ''
        },
        async updated(index) {
            await axios({
                url: 'https://ppdyh35v5v.hzh.sealos.run/update_todo',
                method: 'post',
                data: {
                    id: index
                }
            })
            // 重新获取列表
            this.getList()
        },
        async del(index) {
            // alert('删除')
            // 删除列表调用splice
            // this.list.splice(index, 1)
            await axios({
                url: 'https://ppdyh35v5v.hzh.sealos.run/del_todo',
                method: 'post',
                data: {
                    id: index
                }
            })

            this.getList()
        },
        // 以下使用axiox请求接口
        async getList() {
            var res = await axios({
                // 接口地址
                url: 'https://ppdyh35v5v.hzh.sealos.run/get_list',
                method: 'get',
                // 请求头
                headers: {
                    'Content-Type': 'application/json;charset=UTF-8'
                }
            })
            this.list = res.data.data
            // res.data.data.forEach((item) => {
            //     this.list.push(item)
            // })
        }
    }
}
</script>
<!-- style 样式区域 -->
<style>
.complete {
    text-decoration: line-through;
    opacity: 0.4;
    width: 60%;
    height: 50px;
    box-sizing: border-box;
    border-radius: 20px;
    margin: 8px auto;
    padding: 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 20px;
    box-shadow: rgba(149, 157, 165, 0.2) 0px 8px 20px;
}

.del {
    color: red;
}

.item {
    width: 60%;
    height: 50px;
    box-sizing: border-box;
    border-radius: 20px;
    margin: 8px auto;
    padding: 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 20px;
    box-shadow: rgba(149, 157, 165, 0.2) 0px 8px 20px;
}

.todo-button {
    width: 100px;
    height: 54px;
    line-height: 54px;
    border-radius: 0 20px 20px 0;
    text-align: center;
    background: linear-gradient(
        to right,
        rgb(113, 65, 168),
        rgba(44, 114, 251, 1)
    );
    cursor: pointer;
    user-select: none;
    color: #ffffff;
}

.todo-input {
    width: 60%;
    height: 50px;
    border: 1px solid #dfe1e5;
    outline: none;
    border-radius: 20px 0 0 20px;
    padding-left: 15px;
}

.todo-from {
    display: flex;
    margin-top: 30px;
    margin-bottom: 20px;
    justify-content: center;
    align-items: center;
}

body {
    background: linear-gradient(
        to right,
        rgb(113, 65, 168),
        rgba(44, 114, 251, 1)
    );
}

.todo-app {
    width: 98%;
    height: 500px;
    padding-top: 30px;
    box-sizing: border-box;
    background-color: #ffffff;
    border-radius: 5px;
    margin-top: 40px;
    margin-left: 1%;
}

.title {
    font-size: 30px;
    font-weight: 700;
    text-align: center;
}
</style>
